{% extends 'article_add.html' %}
{% block article_form %}
    <form class="layui-form layui-form-pane" id="editArticleForm">
        {% csrf_token %}
        <div class="layui-form-item">
            <label for="title" class="layui-form-label">随笔标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" autocomplete="off" placeholder="请输入"
                       lay-verify="required"
                       class="layui-input" id="title" value="{{ article_obj.title }}">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label for="create_time" class="layui-form-label">日期选择</label>
                <div class="layui-input-block">
                    <input type="datetime-local" name="create_time" id="create_time" autocomplete="off"
                           lay-verify="required" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章分类</label>
            <div class="layui-input-inline">
                <select name="category" lay-filter="category">
                    <option value=""></option>
                    {% for category in category_all %}
                        {% if category.name == article_obj.category.name %}
                            <option value="{{ category.pk }}" selected>{{ category.name }}</option>
                        {% else %}
                            <option value="{{ category.pk }}">{{ category.name }}</option>
                        {% endif %}
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="tags" class="layui-form-label">文章标签</label>
            {% for tag in tags_all %}
                {% if tag in article_obj.tags.all %}
                    <input type="checkbox" id="tags" name="tags" value="{{ tag.pk }}" title="{{ tag.name }}" checked>
                {% else %}
                    <input type="checkbox" id="tags" name="tags" value="{{ tag.pk }}" title="{{ tag.name }}">
                {% endif %}
            {% endfor %}
        </div>
        <div class="layui-form-item layui-form-text">
            <label for="article_content" class="layui-form-label">文章</label>
            <div class="layui-input-block">
                <textarea id="article_content" name="content" placeholder="请输入内容"
                          class="layui-textarea">{{ article_obj.content }}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit lay-filter="editForm">确认</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
    <script>
        layui.use(['form'], function () {
            let form = layui.form;
            // 提交事件
            form.on('submit(editForm)', function () {
                let formData = new FormData
                $.each($('#editArticleForm').serializeArray(), function (index, data_dict) {
                    console.log(data_dict)
                    formData.append(data_dict.name, data_dict.value)
                })
                // 显示填写结果，仅作演示用
                $.ajax({
                    url: '{% url 'article:edit' request.user.username article_obj.pk %}',
                    type: 'post',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        alert(res.msg)
                        window.location.href = '{% url 'backend:backend' %}'
                    }
                })
                return false; // 阻止默认 form 跳转
            });
        });
    </script>
{% endblock %}